package HttpWsUtils

import (
	"dgo/goutils/utils"
	"gitee.com/ymofen/gobase/golog"
	"net/http"
)

/*
*
 */
func OnWsCbRequest(w http.ResponseWriter, r *http.Request) {
	conn, err := upgrader.Upgrade(w, r, nil)
	if err != nil {
		http.Error(w, err.Error(), 503)
		return
	}
	defer conn.Close()

	wsconn := NewWsConnWrapper(conn, w, r)

	if WsConnOnAcceptEvent != nil {
		if !WsConnOnAcceptEvent(wsconn) {
			return
		}
	}

	for {
		msgtype, recvBuf, err := conn.ReadMessage()
		if err != nil {
			golog.Debugf("read:%s", err)
			break
		} else {
			wsconn.onRecv(msgtype, recvBuf)
		}
	}
}

/*
**

	用CallBack的方式处理消息
*/
func StartProjWsHandleWithCallBack(ws_req_uri string) {
	http.HandleFunc(ws_req_uri, OnWsCbRequest)
}
